package org.jeecg.modules.config;

import org.jeecg.common.util.SpringContextUtils;
import org.jeecg.modules.entity.RunnerMatchPlayer;
import org.jeecg.modules.jmreport.api.data.IDataSetFactory;
import org.jeecg.modules.jmreport.desreport.model.JmPage;
import org.jeecg.modules.service.IRunnerMatchPlayerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @author tanxy
 * @date 2022年12月08日 23:48
 */
@Component
public class MatchRpSpringBean implements IDataSetFactory {
    @Override
    public List<Map<String, Object>> createData(Map<String, Object> map) {
        IRunnerMatchPlayerService playerService = SpringContextUtils.getBean(IRunnerMatchPlayerService.class);
        String matchId = (String)map.get("matchId");
        String playerId = (String)map.get("playerId");
        List<RunnerMatchPlayer> matchPlayers = playerService.selectByMainIdNotEndTime(matchId);
        matchPlayers = matchPlayers.stream().sorted(Comparator.comparing(RunnerMatchPlayer::getPendtime)).collect(Collectors.toList());
        int num = 0;
        String userName = "";
        String group = "";
        String score="";
        RunnerMatchPlayer player = null;
        for (int i = 0; i <matchPlayers.size(); i++) {
            if(matchPlayers.get(i).getId().equals(playerId)){
                player=matchPlayers.get(i);
                num=i+1;
                break;
            }
        }
        if(player!=null){
            userName=player.getPlayer();
            group = player.getGroupid();
            score=player.getScore();
        }
        Map<String,Object> resMap = new HashMap<>();
        resMap.put("num",num);
        resMap.put("userName",userName);
        resMap.put("group",group);
        resMap.put("score",score);
        List<Map<String, Object>> res = new ArrayList<>();
        res.add(resMap);
        return res;
    }

    @Override
    public JmPage createPageData(Map<String, Object> map) {
        return null;
    }
}
